package com.caits.lbs.framework.services.mina;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import com.caits.lbs.framework.log.CommonLogFactory;
 
public class ServerIoHandler extends IoHandlerAdapter {
	private final Logger logger = CommonLogFactory.getLog();
 
	//出现异常的时候调用.
	public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
		logger.warn("Unexpected exception.", cause);  
    }
   //接收客户端新的消息的时候调用.
    public void messageReceived(IoSession session, Object obj) throws Exception {
        //收到的信息
        logger.debug("收到的信息:"+obj.toString());
        session.write("Server Send: " + obj.toString());
        logger.debug("return the state to the client successfull......");
    }

    public void messageSent(IoSession session, Object pArg1) throws Exception {
    	//session.write("通知客户端已经收到信息！！！");
    }
 
    public void sessionClosed(IoSession session) throws Exception {
        
    }
    //当一个客户端连接到服务器的时候被调用.
    public void sessionCreated(IoSession session) throws Exception {
    	logger.debug("Session created...");
    }
    //会话空闲
    public void sessionIdle(IoSession session, IdleStatus pArg1) throws Exception {
        
    }
    //打开会话，与sessionCreated最的区别是它是从另一个线程处调用的。
    public void sessionOpened(IoSession session) throws Exception {
        session.write("[Server: Client,I'm server.]");
    }
 
}
